#!/usr/bin/python3
import sys


print ("hello l love you!")

# 单词对象
class Word:
	def __init__(self, keyWord0, keyWord, sameTime,prePos):
		self.keyWord0 = keyWord0
		self.keyWord = keyWord
		self.sameTime = sameTime
		self.prePos = prePos

# 录入参数校验
if len(sys.argv) < 2:
	print("命令执行的格式不正确")
	print("正确的格式为 python test001.py -alphabet [-length]")
	print("-alphabet 为录入的字母（必输参数）")
	print("-length 最长匹配长度减去的值（可选参数默认为0）")
	sys.exit()
#输入字母
myInput = sys.argv[1]
lenInput = len(myInput)
print ("输入的字母为：" + myInput)
myInput="".join((lambda x:(x.sort(),x)[1])(list(myInput.lower())))
print ("转换后字母为：" + myInput)

#导入词语库
list = [] 
for line in open("words.txt"):
	libWord = line.strip();
	# 过滤超过录入长度的词语
	if len(libWord) >lenInput:
		continue
	word = Word(libWord,libWord.lower(),0,-1)
	#print(word.keyWord)
	list.append(word)

print("库中的词语有：" + str(len(list)) + "条")

# myInput = ["b","e","h","g","m", "m","r", "c", "u"]
#匹配比较
preAl = "";
for al in myInput:
	#print (al)
	for word in list:
		if preAl != al :
			word.prePos = 0
		if word.keyWord.find(al,word.prePos) >= 0:
			word.sameTime += 1
			word.prePos = word.keyWord.find(al,word.prePos)+1
	preAl = al


maxSame = -1;
for word in list:
	if word.sameTime > maxSame and len(word.keyWord) == word.sameTime:
		#print (maxSame)
		#print (word.sameTime)
		maxSame = word.sameTime
		#print(word.keyWord)
print("录入字母数量：" + str(len(myInput)))
print("最大匹配长度：" + str(maxSame))
if len(sys.argv) > 2 :
	maxSame -= int(sys.argv[2])

print("取出匹配长度：" + str(maxSame))

#打印出最匹配的数据
for word in list:
	if word.sameTime == maxSame and len(word.keyWord) == word.sameTime:
		print(word.keyWord0)

print ("Thank you!")
